<?php

namespace Biz\service\springHomeDecActivity;

use Biz\model\springHomeDecActivity\LuckyCharmTicket as mLuckyCharmTicket;
use Biz\pockets\constInterface\SpringHomeDecConfig;
use Kafka\Exception;

class LuckyCharmService implements SpringHomeDecConfig
{

    /**
     * 获取锦鲤
     * @return array
     */
    public static function getLuckCharm()
    {
        $wish = [];
        if (time() < strtotime(self::ACTIVITY_LUCKY_CHARM_PUBLISH_TIME)) {
            return $wish;
        }
        $winningTicket = mLuckyCharmTicket::getWinningTicket();
        if ($winningTicket && is_array($winningTicket)) {
            $wish = WishService::userWish($winningTicket['uid']);
        }
        return $wish;
    }

    /**
     * 设置锦鲤
     * @param $uid
     * @return bool
     */
    public static function setLuckCharm($uid)
    {
        if (time() < strtotime(self::ACTIVITY_TIME_END)) {
            throw new Exception('活动未结束，请结束后设置锦鲤!');
        }

        $where['uid'] = $uid;
        $where['winning'] = mLuckyCharmTicket::WINNING_NO;
        $where['data_status'] = mLuckyCharmTicket::STATUS_NORMAL;

        $ticketInfo = mLuckyCharmTicket::dataSelectInfo($where);

        if (empty($ticketInfo)) {
            throw new Exception('用户没有可以设置锦鲤的锦鲤券！');
        }

        $ticketId = $ticketInfo['id'];

        $nRet = mLuckyCharmTicket::setWinningTicket($ticketId);

        if (!$nRet) {
            return false;
        }

        $wish = WishService::getUserWishInfo($uid);
        if (empty($wish)) {
            return false;
        }

        //更新用户中奖商品
        if (!WishListService::setUserProductWinning($wish['id'])) {
            return false;
        }

        return true;
    }
}